package cn.yunhe.service.impl;

import cn.yunhe.dao.LogDao;
import cn.yunhe.entity.Log;
import cn.yunhe.service.LogService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("logService")
public class LogServiceImpl implements LogService {
    @Resource
    private LogDao logDao;

    @Override
    public int addLog(Log log) {
        if (log.getUserDo()!=null&&log.getUsername()!=null){
            logDao.addLog(log);
        } return 0;
    }

    @Override
    public int delLog(Log log) {
       if (log.getLogId()!=0){
           return logDao.delLog(log);
       }
       return 0;
    }

    @Override
    public Page getAllLogByPage(Map<String, Object> map) {

        Page page = new Page();

        //获取总条数
        page.setTotal(logDao.getAllLogByPageCount(map));
        // System.out.println(knowledgePointDao.getKnowledgePointListCountByPage(map));
        // 获取每页大小
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        page.setPageSize(pageSize);

        // 获取当前页码
        int curPageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
//		page.setPageNum(curPageNum);
        // 计算出总页数
        page.setPageNum((int) (page.getTotal() / pageSize + (page.getTotal() % pageSize == 0 ? 0 : 1)));
        // 启用分页
        page = PageHelper.startPage(curPageNum, pageSize);
        // 查询列表
        List list = logDao.getAllLogByPage(map);
        return page;
    }
}
